
package com.viweb.storemanager.core;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class ImageUtil {

    /**
     * This method is used to resize the image.
     * @param srcFile the path of source file
     * @param width the width which we want to resize to
     * @param height the height which we want to resize to
     * @param path the destination that we want to save image file after resize
     */
    public void resizeImage(String srcFile, int width, int height, String path) {
        if (existFile(srcFile)) {
            BufferedImage image;
            try {
                image = ImageIO.read(new File(srcFile));
                Image rescaled = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
                BufferedImage biRescaled = toBufferedImage(rescaled, BufferedImage.TYPE_INT_RGB);
                ImageIO.write(biRescaled, FileUtil.getExtension(srcFile), new File(path));

            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }

    }

    /**
     * This function used test existed of the file
     * @param sFilePath the path of file include also file name
     * @return true? existed : not existed
     */
    private boolean existFile(String sFilePath) {
        boolean existed = false;
        try {
            File file = new File(sFilePath);
            if (file.exists()) {
                existed = true;
            }
        } catch (Exception ex) {
            existed = false;
        }
        return existed;
    }

    public static GraphicsConfiguration getDefaultConfiguration() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        return gd.getDefaultConfiguration();
    }

    public static BufferedImage toBufferedImage(Image image, int type) {
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        BufferedImage result = new BufferedImage(w, h, type);
        Graphics2D g = result.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return result;
    }

    /**
     * This function used resize image
     * @return obj[2]: the obj[0] is width and obj[1] is height of the image
     */
    public Object[] getImageSize(String path, String imageName) {
        Object[] obj = new Object[2];
        try {
            Image inImage = new ImageIcon(path + "/" + imageName).getImage();
            int maxDim = 120;
            double scale = (double) maxDim / (double) inImage.getHeight(null);
            if (inImage.getWidth(null) > inImage.getHeight(null)) {
                scale = (double) maxDim / (double) inImage.getWidth(null);
            }
            // Determine size of new image.
            obj[0] = (int) (scale * inImage.getWidth(null));
            obj[1] = (int) (scale * inImage.getHeight(null));
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return obj;
    }

    public static ImagePanel getImagePanel(int w, int h, byte[] imageData) {
        try {
            BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(imageData));
            ImagePanel imagePanel = new ImagePanel(bufferedImage, ImagePanel.SCALED);
            imagePanel.setPreferredSize(new Dimension(w, h));

            return imagePanel;
        } catch (Exception ex) {
        }

        return null;
    }
}
